﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Use this to generate a simple per pixel filter. 
    /// So all you have to do is to create a simple delegate.
    /// This delegate is used on all pixels then.
    /// </summary>
    public class PerPixelFilter : BaseImageFilter, ITwoSourceFilter
    {
        public delegate Color PerPixelFilterDelegate(Color _color);
        public delegate Color PerPixelFilterDelegateTwoSources(Color _color1, Color _color2);

        protected PerPixelFilterDelegate m_PerPixelFilterDelegate = null;
        protected PerPixelFilterDelegateTwoSources m_PerPixelFilterDelegateTwoSources = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        public PerPixelFilter()
        {
            m_PerPixelFilterDelegate = new PerPixelFilterDelegate(
                delegate(Color _color)
                {
                    return ColorFunctions.Gray(_color) > 127 ? Definitions.Black : Definitions.White;
                });

            m_PerPixelFilterDelegateTwoSources = new PerPixelFilterDelegateTwoSources(
                delegate(Color _color1, Color _color2)
                { 
                    return Color.FromArgb(Math.Max(_color1.R - _color2.G, 0)); 
                });
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        /// <param name="_perPixelFilterDelegate">The _per pixel filter delegate.</param>
        public PerPixelFilter(PerPixelFilterDelegate _perPixelFilterDelegate)
        {
            m_PerPixelFilterDelegate = _perPixelFilterDelegate;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        /// <param name="_perPixelFilterDelegate">The _per pixel filter delegate.</param>
        public PerPixelFilter(PerPixelFilterDelegateTwoSources _perPixelFilterDelegateTwoSources)
        {
            m_PerPixelFilterDelegateTwoSources = _perPixelFilterDelegateTwoSources;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        /// <param name="_perPixelFilter">The _per pixel filter.</param>
        internal PerPixelFilter(PerPixelFilter _perPixelFilter)
        {
            m_PerPixelFilterDelegate = _perPixelFilter.m_PerPixelFilterDelegate;
        }

        /// <summary>
        /// Gets or sets the per pixel filter delegate.
        /// </summary>
        /// <value>The per pixel filter delegate.</value>
        public PerPixelFilterDelegate FilterDelegate
        {
            get { return m_PerPixelFilterDelegate; }
            set { m_PerPixelFilterDelegate = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            Color color = Color.Transparent;
            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    _bitmap.SetPixel(x, y, m_PerPixelFilterDelegate(_bitmap.GetPixel(x, y)));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new PerPixelFilter(this);
        }

        /// <summary>
        /// Use this if you'd like to execute a two filter per pixel delegate
        /// </summary>
        /// <param name="_bitmapSource"></param>
        /// <param name="_bitmapDest"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmapSource, UnsafeBitmap _bitmapDest)
        {
            int nWidth = _bitmapSource.Width;
            int nHeight = _bitmapSource.Height;
            Color color = Color.Transparent;
            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    _bitmapDest.SetPixel(x, y, m_PerPixelFilterDelegateTwoSources(_bitmapSource.GetPixel(x, y), _bitmapDest.GetPixel(x,y)));
                }
            }
            _bitmapSource.Dispose();
            _bitmapDest.Dispose();
            return _bitmapDest;
        }
    }
}
